<template>
  <div class="app-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="如若不适应手机端操作，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="table-container">
      <div style="padding-bottom:20px; display: flex;">
        <el-button type="primary" size="medium" plain @click="addthesis">学科建设录入</el-button>
        <div style="width: 200px; margin-left: 20px;">
          <el-input v-model="search" size="medium" placeholder="输入关键字搜索" />
        </div>
        <div style="margin-left: 20px;" />
        <el-button type="success" size="medium" plain icon="el-icon-download" @click="excel_dialog = true">导出</el-button>
      </div>
      <el-table
        v-loading="paperListLoading"
        :data="paperList"
        element-loading-text="Loading"
        border
        fit
      >
        <el-table-column type="selection" align="center" width="55" />
        <!-- <el-table-column align="center" label="序号" width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="负责人" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.head }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.projectName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="学科类型" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.disciplineType }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="统计分类" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.classifi }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系地址" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.address }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
          :filters="[{ text: '草稿', value: '草稿' }, { text: '待院审核', value: '待院审核' }, { text: '被院级退回', value: '被院级退回' }, { text: '待校审核', value: '待校审核' }, { text: '被校级退回', value: '被校级退回' }, { text: '审核通过', value: '审核通过' }]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
            <el-tag v-if="scope.row.status === '被院级退回' | scope.row.status === '被校级退回'" type="warning">{{ scope.row.reason }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-popover
              :ref="`popoverdelete-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认删除本行吗？该操作不可撤销</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popoverdelete-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="delete_thesis(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-popover
              :ref="`popovercommit-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认提交吗？提交后不可再修改</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popovercommit-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="commit_thesis(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-button
              size="mini"
              @click="open_infoview_dialog(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-popover="`popoverdelete-${scope.$index}`"
              size="mini"
              type="danger"
            >
              删除
            </el-button>
            <el-button
              v-popover="`popovercommit-${scope.$index}`"
              size="mini"
              type="success"
            >
              提交
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :current-page="p1_page"
          :page-sizes="[1,2,3,10, 20, 30, 40]"
          :page-size="p1_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="p1_total"
          @size-change="p1_sizechange"
          @current-change="p1_currentchange"
        />
        <!-- @size-change="handleSizeChange"
          @current-change="handleCurrentChange" -->
        <!-- 使用了size-change和current-change事件来处理页码大小和当前页变动时候触发的事件。 -->
      </div>
      <el-dialog title="学科建设录入" :visible.sync="thesis_add_dialog" width="75%" :close-on-click-modal="false">
        <el-form ref="thesis_add_form1" :model="thesis_add_form1" label-position="top" label-width="160px" autocomplete="off">
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="录入人" prop="entered">
                <el-input v-model="thesis_add_form1.entered" :disabled="true" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="负责人" prop="head">
                <el-input v-model="thesis_add_form1.head" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="录入日期" prop="enteredDate">
                <el-date-picker
                  v-model="thesis_add_form1.enteredDate"
                  type="date"
                  placeholder="选择日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="项目名称" prop="projectName">
                <el-input v-model="thesis_add_form1.projectName" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="统计分类" prop="classifi">
                <el-input v-model="thesis_add_form1.classifi" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="学科类型" prop="disciplineType">
                <el-select v-model="thesis_add_form1.disciplineType" placeholder="请选择" style="width: 270px;">
                  <el-option label="学科类型" value="学科类型" />
                  <el-option label="学科类型" value="学科类型" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="联系地址" prop="address">
                <el-input v-model="thesis_add_form1.address" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="联系电话" prop="phone">
                <el-input v-model="thesis_add_form1.phone" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="email" prop="email">
                <el-input v-model="thesis_add_form1.email" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="邮政编码" prop="zipcode">
                <el-input v-model="thesis_add_form1.zipcode" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="批准年度" prop="approvalYear">
                <el-input v-model="thesis_add_form1.approvalYear" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="批准批次" prop="approvalBatch">
                <el-input v-model="thesis_add_form1.approvalBatch" style="min-width: 270px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="备注" prop="notes">
                <el-input v-model="thesis_add_form1.notes" type="textarea" style="min-width: 270px; width:300px;" />
              </el-form-item>
            </el-col>
            <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <br>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-popover
            ref="dialog1"
            v-model="visible1"
            placement="top"
            width="160"
          >
            <p>关闭后，已填写的内容会丢失，建议保存为草稿，确定要关闭吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="visible1 = false">取消</el-button>
              <el-button type="primary" size="mini" @click="visible1 = false;thesis_add_dialog = false;">确定</el-button>
            </div>
          </el-popover>
          <el-button v-popover:dialog1>关 闭</el-button>
          <el-button type="primary" @click="save_add_form1(thesis_add_form1)">保 存</el-button>
        </div>
      </el-dialog>
      <el-dialog title="编辑信息" :visible.sync="infoview_dialog" width="75%">
        <el-collapse v-model="activeNames">
          <el-collapse-item name="1">
            <template slot="title">
              <div class="el-descriptions__title">学科建设信息</div>
            </template>
            <el-dialog title="学科建设信息" :visible.sync="thesis_info_dialog" width="60%" append-to-body :close-on-click-modal="false">
              <el-form :model="thesis_add_form2" label-position="top" label-width="160px" autocomplete="off">
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="录入人" prop="entered">
                      <el-input v-model="thesis_add_form2.entered" :disabled="true" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="负责人" prop="head">
                      <el-input v-model="thesis_add_form2.head" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="录入日期" prop="enteredDate">
                      <el-date-picker
                        v-model="thesis_add_form2.enteredDate"
                        type="date"
                        placeholder="选择日期"
                        format="yyyy 年 MM 月 dd 日"
                        value-format="yyyy-MM-dd"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="项目名称" prop="projectName">
                      <el-input v-model="thesis_add_form2.projectName" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="统计分类" prop="classifi">
                      <el-input v-model="thesis_add_form2.classifi" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="学科类型" prop="disciplineType">
                      <el-select v-model="thesis_add_form2.disciplineType" placeholder="请选择" style="width: 270px;">
                        <el-option label="学科类型" value="学科类型" />
                        <el-option label="学科类型" value="学科类型" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="联系地址" prop="address">
                      <el-input v-model="thesis_add_form2.address" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="联系电话" prop="phone">
                      <el-input v-model="thesis_add_form2.phone" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="email" prop="email">
                      <el-input v-model="thesis_add_form2.email" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="邮政编码" prop="zipcode">
                      <el-input v-model="thesis_add_form2.zipcode" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="批准年度" prop="approvalYear">
                      <el-input v-model="thesis_add_form2.approvalYear" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="批准批次" prop="approvalBatch">
                      <el-input v-model="thesis_add_form2.approvalBatch" style="min-width: 270px;" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
                    <el-form-item label="备注" prop="notes">
                      <el-input v-model="thesis_add_form2.notes" type="textarea" style="min-width: 270px; width:300px;" />
                    </el-form-item>
                  </el-col>
                  <el-col :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
                    <br>
                  </el-col>
                </el-row>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-popover
                  ref="dialog2"
                  v-model="visible2"
                  placement="top"
                  width="160"
                >
                  <p>关闭后，已填写的内容会丢失，建议保存为草稿，确定要关闭吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="visible2 = false">取消</el-button>
                    <el-button type="primary" size="mini" @click="visible2 = false;thesis_info_dialog = false;">确定</el-button>
                  </div>
                </el-popover>
                <el-button v-popover:dialog2>关 闭</el-button>
                <el-button type="primary" @click="save_add_form2(thesis_add_form2)">保 存</el-button>
              </div>
            </el-dialog>
            <div style="padding-bottom:20px; display: flex;">
              <el-button type="primary" size="medium" plain @click="open_thesis_info_dialog(infoview1)">编辑信息</el-button>
            </div>
            <el-descriptions :column="2" size="medium" border :label-style="{width:'25%'}" :content-style="{width:'25%'}">
              <el-descriptions-item label="学科建设ID">
                {{ infoview1.constructionID }}
              </el-descriptions-item>
              <el-descriptions-item label="录入人">
                {{ infoview1.entered }}
              </el-descriptions-item>
              <el-descriptions-item label="学科负责人/带头人">
                {{ infoview1.head }}
              </el-descriptions-item>
              <el-descriptions-item label="录入日期">
                {{ infoview1.enteredDate }}
              </el-descriptions-item>
              <el-descriptions-item label="项目名称">
                {{ infoview1.projectName }}
              </el-descriptions-item>
              <el-descriptions-item label="统计分类">
                {{ infoview1.classifi }}
              </el-descriptions-item>
              <el-descriptions-item label="学科类型">
                {{ infoview1.disciplineType }}
              </el-descriptions-item>
              <el-descriptions-item label="联系地址">
                {{ infoview1.address }}
              </el-descriptions-item>
              <el-descriptions-item label="联系电话">
                {{ infoview1.phone }}
              </el-descriptions-item>
              <el-descriptions-item label="email">
                {{ infoview1.email }}
              </el-descriptions-item>
              <el-descriptions-item label="邮政编吗">
                {{ infoview1.zipcode }}
              </el-descriptions-item>
              <el-descriptions-item label="批准年度">
                {{ infoview1.approvalYear }}
              </el-descriptions-item>
              <el-descriptions-item label="批准批次">
                {{ infoview1.approvalBatch }}
              </el-descriptions-item>
              <el-descriptions-item label="状态">
                <el-tag :type="infoview1.status | statusFilter">{{ infoview1.status }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="未通过原因">
                {{ infoview1.reason }}
              </el-descriptions-item>
              <el-descriptions-item label="备注">
                {{ infoview1.notes }}
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
          <el-collapse-item name="2">
            <template slot="title">
              <div class="el-descriptions__title">相关附件</div>
            </template>
            <div class="upload">
              <div class="uploadBox">
                <el-upload
                  ref="upload"
                  class="upload-demo"
                  drag
                  action="http://118.178.135.35:8085/fileUpload/fileUpload"
                  accept=".jpg,.png,.doc,.docx,.xls,.xlsx,.pdf"
                  :on-success="handleFileSuccess"
                  :show-file-list="true"
                >
                  <i class="el-icon-upload" />
                  <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png/doc/xls/pdf/docx/xlsx文件，且不超过2MB</div>
                </el-upload>
              </div>
              <div class="uploadText">
                <el-alert
                  title="注意事项"
                  type="warning"
                  :closable="false"
                  :show-icon="true"
                >
                  <p>1、一次上传一个文件，上传完成的文件会在下方表格中显示</p>
                  <p>2、在下方表格中您可以进行下载和删除操作</p>
                  <p>3、本模块您需要上传的附件有“XXXXX”、“XXXXXX”</p>
                  <p>4、请严格按照文件命名规则来命名文件</p>
                </el-alert>
              </div>
            </div>
            <el-table
              v-loading="fileListLoading"
              :data="fileList"
              element-loading-text="Loading"
              border
              fit
            >
              <el-table-column type="index" align="center" width="55" />
              <el-table-column align="left" label="文件名称" min-width="150">
                <template slot-scope="scope">
                  {{ scope.row.filename }}
                </template>
              </el-table-column>
              <el-table-column align="left" label="文件大小" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.filesize / 1024 / 1024 < 1">{{ (scope.row.filesize / 1024).toFixed(2) + 'KB' }}</span>
                  <span v-else>{{ (scope.row.filesize / 1024 / 1024).toFixed(2) + 'MB' }}</span>
                </template>
              </el-table-column>
              <el-table-column align="left" label="上传时间" min-width="150">
                <template slot-scope="scope">
                  {{ scope.row.filedate }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="150">
                <template slot-scope="scope">
                  <el-popover
                    :ref="`popoverdelete-${scope.$index}`"
                    placement="top"
                    width="160"
                  >
                    <p>确认删除本行吗？该操作不可撤销</p>
                    <div style="text-align: right; margin: 0">
                      <el-button size="mini" type="text" @click="scope._self.$refs[`popoverdelete-${scope.$index}`].doClose()">取消</el-button>
                      <el-button type="primary" size="mini" @click="deleteFile(scope.$index, scope.row)">确定</el-button>
                    </div>
                  </el-popover>
                  <el-button
                    size="mini"
                    @click="downloadFile(scope.row)"
                  >
                    下载
                  </el-button>
                  <el-button
                    v-popover="`popoverdelete-${scope.$index}`"
                    size="mini"
                    type="danger"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
        </el-collapse>
        <div slot="footer" class="dialog-footer">
          <el-button @click="infoview_dialog = false">关 闭</el-button>
        </div>
      </el-dialog>
      <el-dialog title="导出excel" :visible.sync="excel_dialog" width="60%">
        <el-alert
          title="请选择需要导出的数据"
          type="success"
          description="选择的数据为表头，未选择的数据将不会导出"
          :closable="false"
        />
        <div style="margin: 15px;">
          <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
          <div style="margin: 15px 0;" />
          <el-checkbox-group v-model="check_fields" @change="handleCheckedFieldsChange">
            <el-checkbox v-for="field in fields_list" :key="field" :label="field">{{ field }}</el-checkbox>
          </el-checkbox-group>
        </div>
        <el-alert
          title="请选择时间范围"
          type="success"
          description="选择导出数据的时间范围"
          :closable="false"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="excel_dialog = false">关 闭</el-button>
          <download-excel
            style="display:inline-block !important;margin-left:10px;"
            :data="EXCEL.DetailsForm"
            :fields="EXCEL.json_fields"
            :header="EXCEL.title"
            type="xls"
            name="学科建设数据导出.xls"
            :fetch="createExportData"
            :before-generate="startDownload"
            :before-finish="finishDownload"
          >
            <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
            <el-button type="success" plain>确认导出</el-button>
          </download-excel>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getMyPaperList, thesis_add, thesis_add2, thesis_delete, thesis_commit, get_file, file_add, file_delete } from '@/api/discipline'
import { Message, Loading } from 'element-ui'
import { mapGetters } from 'vuex'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        草稿: 'info',
        待院审核: '',
        被院级退回: 'danger',
        待校审核: '',
        被校级退回: 'danger',
        审核通过: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: '',
      thesis_add_dialog: false,
      thesis_info_dialog: false,
      infoview_dialog: false,
      visible1: false,
      visible2: false,
      thesis_add_form1: {
        address: '',
        annex: '',
        approvalBatch: '',
        approvalYear: '',
        classifi: '',
        constructionID: '',
        disciplineType: '',
        email: '',
        entered: '',
        enteredDate: '',
        head: '',
        notes: '',
        phone: '',
        projectName: '',
        reason: '',
        status: '',
        zipcode: ''
      },
      thesis_add_form2: {
        address: '',
        annex: '',
        approvalBatch: '',
        approvalYear: '',
        classifi: '',
        constructionID: '',
        disciplineType: '',
        email: '',
        entered: '',
        enteredDate: '',
        head: '',
        notes: '',
        phone: '',
        projectName: '',
        reason: '',
        status: '',
        zipcode: ''
      },
      paperList: null,
      paperListLoading: false,
      p1_size: 10,
      p1_total: 0,
      p1_page: 1,
      infoview1: {
        address: '',
        annex: '',
        approvalBatch: '',
        approvalYear: '',
        classifi: '',
        constructionID: '',
        disciplineType: '',
        email: '',
        entered: '',
        enteredDate: '',
        head: '',
        notes: '',
        phone: '',
        projectName: '',
        reason: '',
        status: '',
        zipcode: ''
      },
      activeNames: ['1', '2'],
      excel_dialog: false,
      all_fields: {
        'ID': 'constructionID',
        '录入人': 'entered',
        '学科负责人/带头人': 'head',
        '录入日期': 'enteredDate',
        '项目名称': 'projectName',
        '统计分类': 'classifi',
        '学科类型': 'disciplineType',
        '联系地址': 'address',
        '联系电话': 'phone',
        'email': 'email',
        '邮政编码': 'zipcode',
        '批准年度': 'approvalYear',
        '批准批次': 'approvalBatch',
        '审核状态': 'status',
        '未通过原因': 'reason',
        '备注': 'notes'
      },
      fields_list: [
        'ID',
        '录入人',
        '学科负责人/带头人',
        '录入日期',
        '项目名称',
        '统计分类',
        '学科类型',
        '联系地址',
        '联系电话',
        'email',
        '邮政编码',
        '批准年度',
        '批准批次',
        '审核状态',
        '未通过原因',
        '备注'
      ],
      check_fields: [],
      checkAll: false,
      isIndeterminate: false,
      EXCEL: {
        title: '测试表格',
        json_fields: null,
        DetailsForm: null
      },
      fileList: [],
      fileListLoading: false,
      fileForm: {
        constructionID: '',
        filedate: '',
        fileid: '',
        filename: '',
        filesize: '',
        fileurl: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ]),
    MyPaperPost() {
      return {
        name: this.name,
        pageNum: this.p1_page,
        pageSize: this.p1_size
      }
    }
  },
  created() {
    this.fetchPaperData()
  },
  methods: {
    fetchPaperData() {
      this.paperListLoading = true
      getMyPaperList(this.MyPaperPost).then(response => {
        if (response.code === 200) {
          this.paperList = response.data.list
          this.p1_total = response.data.total
          this.paperListLoading = false
        }
      })
    },
    p1_sizechange(newsize) {
      this.p1_size = newsize
      this.p1_page = 1
      this.fetchPaperData()
    },
    p1_currentchange(newpage) {
      this.p1_page = newpage
      this.fetchPaperData()
    },
    filterTag(value, row) {
      return row.status === value
    },
    resetForm(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
    },
    addthesis() {
      this.thesis_add_dialog = true
      this.resetForm('thesis_add_form1')
      this.thesis_add_form1.entered = this.name
    },
    save_add_form1(formdata) {
      thesis_add(formdata).then(response => {
        if (response.code === 200) {
          Message.success('保存成功')
          this.thesis_add_dialog = false
          this.fetchPaperData()
        }
      })
    },
    open_thesis_info_dialog(data) {
      this.thesis_info_dialog = true
      this.resetForm('thesis_add_form2')
      this.thesis_add_form2 = JSON.parse(JSON.stringify(data))
    },
    save_add_form2(formdata) {
      thesis_add2(formdata).then(response => {
        if (response.code === 200) {
          Message.success('保存成功')
          this.thesis_info_dialog = false
          this.fetchPaperData()
          this.infoview1 = formdata
        }
      })
    },
    delete_thesis(index, row) {
      this.$refs[`popoverdelete-${index}`].doClose()
      thesis_delete(row.constructionID).then(response => {
        if (response.code === 200) {
          Message.success('删除成功')
          this.fetchPaperData()
        }
      })
    },
    commit_thesis(index, row) {
      this.$refs[`popovercommit-${index}`].doClose()
      thesis_commit(row.constructionID).then(response => {
        if (response.code === 200) {
          Message.success('提交成功')
          this.fetchPaperData()
        }
      })
    },
    open_infoview_dialog(row) {
      this.infoview_dialog = true
      this.infoview1 = JSON.parse(JSON.stringify(row))
      this.fileForm.constructionID = row.constructionID
      this.fetchFileData()
    },
    // 以下为数据导出
    handleCheckAllChange(val) {
      this.check_fields = val ? this.fields_list : []
      this.isIndeterminate = false
      this.EXCEL.json_fields = JSON.parse(JSON.stringify(this.all_fields, this.check_fields))
    },
    handleCheckedFieldsChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.fields_list.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.fields_list.length
      this.EXCEL.json_fields = JSON.parse(JSON.stringify(this.all_fields, this.check_fields))
    },
    async createExportData() {
      return await getMyPaperList(this.MyPaperPost).then(response => {
        if (response.code === 200) {
          if (response.data.list.length === 0) {
            Message.warning('返回数据为空，导出失败！')
            this.loadingInstance.close()
          }
          return response.data.list
        }
      })
    },
    startDownload() {
      this.excel_dialog = false
      this.loadingInstance = Loading.service({ text: '正在导出...' })
    },
    finishDownload() {
      setTimeout(() => {
        this.loadingInstance.close()
      }, 1000)
    },
    // 以下是附件上传
    handleFileSuccess(res, file, fileList) {
      this.fileForm.filename = file.name
      this.fileForm.filesize = file.size
      this.fileForm.fileurl = res.data
      this.commit_file()
    },
    commit_file() {
      file_add(this.fileForm).then(response => {
        if (response.code === 200) {
          Message.success('上传成功')
          this.fetchFileData()
        }
      })
    },
    fetchFileData() {
      this.fileListLoading = true
      get_file(this.fileForm.constructionID).then(response => {
        if (response.code === 200) {
          this.fileList = response.data
          this.fileListLoading = false
        }
      })
    },
    downloadFile(row) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.href = row.fileurl
      a.target = '_blank'
      a.dispatchEvent(event)
    },
    deleteFile(index, row) {
      this.$refs[`popoverdelete-${index}`].doClose()
      file_delete(row.fileid).then(response => {
        if (response.code === 200) {
          Message.success('删除成功')
          this.fetchFileData()
        }
      })
    }
  }
}
</script>

<style lang="scss">
.el-form{
  margin-left: 40px;
  .el-input,.el-select{
    width: 300px;
  }
}
.el-tag + .el-tag {
  margin: 10px 10px;
}
.margin-top {
  margin-top: 50px;
}

@media screen and (max-width:600px) {
  .el-dialog{
    width: 100% !important;
  }
  .el-form{
    margin-left: 25px;
  }
}
</style>
